function requestFeeds() {
    var deferred = getFeeds();
   
    deferred.done(function(data) {
    	populateList(data);   // "data" is the xml document!
     }).fail(function(data) {
         console.log(data); // data is error
     }).always(function(data) {
          // something to always do [optional]
     });
}

function getSavedFeeds()
{
	var deferred = requestSavedFeeds();
	
	deferred.done(function(items) {
		populateList(items, true);
	});
}

function populateList(data, hideSaveButton)
{
	 var items = $(data).find("item");
	 
	 if (items.length === 0) {
		 items = data;
	 }
	 
	 feedListData = items;
	 
	 var template = ['<li data-feed="{0}")>',
	                	'<a href="feed.htm#id={0}" data-transition="slide">{1}</a>',
                		'<div></div>','<span class="button-container">'];//between the <div> tag was{2}
	if(!hideSaveButton)
	{
		template.push('<span class="button-container">');
			template.push('<button class="btn btn-medium btn-primary save-feed">Save</button>');
		template.push('</span>');
	}
	
	template.push('</span>');
	template.push('</li>') ;
	             
	 populateElement(items, "#items", template.join(""), "id,title,description");
}

function populateSingleFeed() {
	readFeed(".well", "<h2><a href=\"{0}\" target=\"_blank\">{1}</a></h2><hr><div class=\"feed-content\">{2}</div>", "link,title,description");
}

function bindSaveButtonEvents() {
	$(document).on("click", ".save-feed", function(event) {
		var holder = $(event.target).closest("li");
		var feedId = $(holder).attr("data-feed"); 
		
		saveFeed(feedId).done(function() {
			$(holder).animate({
				height:0
			}, function() {
					$(holder).remove();
				}
			);
		});
	});
}

function login(username, password) {
	var deferred = tryLogin(username, password);
	
	deferred.done(function(response) {
//		var savedDeferred = requestSavedFeeds();
//		savedDeferred.done(function(data) {
//			initSavedFeeds(data).always(function(){
//				window.location = "FeedsList.htm";
//			});
//			//window.location = "FeedsList.htm";
//		}).fail(function() {
//			window.location = "FeedsList.htm";
//		});
		window.location = "FeedsList.htm";
	}).fail(function(response) {
	});
}

function bindLogout() {
	
	 $(document).off("click", ".logout-button").on("click", ".logout-button", function() {
	
		var deferred =requsetLogOutServer();
		deferred.done(function(response) {
			window.location = "index.htm";
		}).fail(function(response) {
			
		});
	 });
	
}

function signUp() {
	var deferred = trysignUp();
	
	deferred.done(function(response) {
		
		
	}).fail(function(response) {
	});
}

function bindAddFeedEvents() {
	$(document).off("click", "#add-feed").on("click", "#add-feed", function() {
		var deferred = addFeedToServer();
		deferred.done(function(response){
			$("#rss-url").val("");
			$("#rss-name").val("");
		}).always(function(response) {
			var isError = !response || (response && !response.status);
			var message = response.msg || ((isError)? "Error" : "Saved");
			var cssClass = (isError)? "error" : "success";
			
			setAddFeedAlert(message, cssClass);
			//"RSS Added successfully"
		});
	});
	
	$(document).off("click", "#cancel-feed").on("click", "#cancel-feed", function() {
		$("#add-feed-error").text("").removeClass();
		$("#rss-name").val("");
	    $("#rss-url").val("");
	});
}

function validateLoggedInUser() {
	var isUserLoggedIn = isLoggedIn();
	if (!isUserLoggedIn) {
		window.location = "index.htm";
	}
	
	return isUserLoggedIn;
}

function showLoader(message) {
	if ($(".page-loader").length == 0) {
		var loader = $("<div>").attr("id","page-loader").addClass("page-loader").css("display", "none");
		var textHolder = $("<div>").addClass("loader-text");
		
		textHolder.text(message);
		loader.append(textHolder);
		$(document.body).append(loader);
		loader.fadeIn("fast");
	}
}

function hideLoader() {
	$(".page-loader").fadeOut("fast", function() {
		$(".page-loader").remove();
	});
}

$(function() {
	bindLogout();
});
